<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class mdl_jadwal_kuliah extends CI_Model {
    
	function __construct(){
        parent::__construct();
    }
	
	function getdata($plimit=true){
		# get parameter from easy grid
		$page = isset($_POST['page']) ? intval($_POST['page']) : 1;  
		$limit = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'a.id_jadwal';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		$offset = ($page-1)*$limit;
		
		#get filter
		$id_tahunajaran = isset($_POST['id_tahunajaran']) ? strval($_POST['id_tahunajaran']) : '';
		//$kd_fakultas = isset($_POST['kd_fakultas']) ? strval($_POST['kd_fakultas']) : '';
		$kd_prodi = isset($_POST['kd_prodi']) ? strval($_POST['kd_prodi']) : '';
		$kd_matakuliah = isset($_POST['kd_matakuliah']) ? strval($_POST['kd_matakuliah']) : '';
		$kd_dosen = isset($_POST['kd_dosen']) ? strval($_POST['kd_dosen']) : '';
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.id_jadwal, a.id_hari, a.JamMulai, a.JamSelesai, b.Nama as nama_ruang, a.kd_matakuliah, a.Nama, a.kd_kelas, a.kd_jenisjadwal, a.SKS, c.nama_dosen, a.JumlahMhsw', false);
			$this->db->from('tbl_jadwal a');
			$this->db->join('ref_ruang b', 'b.kd_ruang = a.kd_ruang');
			$this->db->join('ref_dosen c', 'c.kd_dosen = a.kd_dosen');
			
			// if($kd_fakultas != '')
				// $this->db->where('a.kd_fakultas', $kd_fakultas);
			//if($kd_prodi != '')
				$this->db->where('a.kd_prodi', $kd_prodi);
			//if($id_tahunajaran != '')
				$this->db->where('a.id_tahunajaran', $id_tahunajaran);
				
			if($kd_matakuliah != ''){
				$this->db->where('a.kd_matakuliah', $kd_matakuliah);
			}
			
			if($kd_dosen != ''){
				$this->db->where('a.kd_dosen', $kd_dosen);
			}
				
			$this->db->where('a.status', 'A');
			
			$this->db->order_by('a.id_hari', 'asc');
			$this->db->order_by('a.JamMulai', 'asc');
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		if($plimit == true){
			$this->db->limit($limit, $offset);
		}
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function getdata_detail($id){
		$sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'nama_matakuliah';  
		$order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';  
		
		# create query
		$this->db->flush_cache();
		$this->db->start_cache();
			$this->db->select('a.kd_matakuliah, b.nama_matakuliah, b.sks, b.semester');
			$this->db->from('tbl_krs_detail a');
			$this->db->join('ref_matakuliah b', 'b.kd_matakuliah = a.kd_matakuliah');
			$this->db->where('a.id_krs', $id);
			$this->db->order_by($sort, $order);
		$this->db->stop_cache();
		
		# get count
		$tmp['row_count'] = $this->db->get()->num_rows();
		
		# get data
		$tmp['row_data'] = $this->db->get();
		
		return $tmp;
	}
	
	function togrid($data, $count){
		$response->total = $count;
		$response->rows = array();
		if($count>0){
			$i=0;
			foreach($data->result_array() as $row){
				foreach($row as $key => $value){
					$response->rows[$i][$key]=$value;
				}
				$i++;
			}
		}
		return json_encode($response);
	}
	
	/*
	function isExistKode($kode){
		if ($kode!=null)
			$this->db->where('id_tahunajaran', $kode['id_tahunajaran'].$kode['semester']);
		
		$this->db->select('*');
		$this->db->from('tbl_tahunajaran');
		$query = $this->db->get();
		
		$rs = $query->num_rows() ;		
		$query->free_result();
		
		return ($rs>0);
	}*/
	
	function getdataedit($id){
		$this->db->flush_cache();
		$this->db->select('a.*, b.kd_fakultas, c.kd_kurikulum');
		$this->db->from('tbl_jadwal a');
		$this->db->join('ref_prodi b', 'b.kd_prodi = a.kd_prodi');
		$this->db->join('ref_matakuliah c', 'c.kd_matakuliah = a.kd_matakuliah');
		$this->db->where('a.id_jadwal', $id);
		return $this->db->get();
	}
	
	function getMatakuliah($kd_matakuliah){
		$this->db->flush_cache();
		$this->db->select('nama_matakuliah, sks');
		$this->db->from('ref_matakuliah');
		$this->db->where('kd_matakuliah', $kd_matakuliah);
		$res = $this->db->get();
		
		return $res;
	}
	
	function InsertOnDb($data){
		//query insert data		
		$this->db->flush_cache();
		// $this->db->set('id_jadwal', $data['id_jadwal']);
        // $this->db->set('JadwalPar', $data['JadwalPar']);
        // $this->db->set('JadwalSer', $data['JadwalSer']);
        $this->db->set('id_tahunajaran', $data['id_tahunajaran']);
        $this->db->set('kd_pt', $this->mdl_prosedur->getKodePT()); //inject
        $this->db->set('kd_prodi', $data['kd_prodi']);
        $this->db->set('kd_kelas', $data['kd_kelas']);
        $this->db->set('kd_jenisjadwal', $data['kd_jenisjadwal']);
        //$this->db->set('AdaResponsi', $data['AdaResponsi']);
        $this->db->set('kd_matakuliah', $data['kd_matakuliah']);
        $this->db->set('Nama', $data['Nama']);
        $this->db->set('id_hari', $data['id_hari']);
        $this->db->set('JamMulai', $data['JamMulai']);
        $this->db->set('JamSelesai', $data['JamSelesai']);
        // $this->db->set('TglMulai', $data['TglMulai']);
        // $this->db->set('TglSelesai', $data['TglSelesai']);
        $this->db->set('SKS', $data['SKS']);
        //$this->db->set('SKSHonor', $data['SKSHonor']);
        $this->db->set('kd_dosen', $data['kd_dosen']);
        $this->db->set('RencanaKehadiran', $data['RencanaKehadiran']);
        //$this->db->set('Kehadiran', $data['Kehadiran']);
        $this->db->set('KehadiranMin', $data['KehadiranMin']);
        // $this->db->set('JumlahMhsw', $data['JumlahMhsw']);
        // $this->db->set('JumlahMhswKRS', $data['JumlahMhswKRS']);
        // $this->db->set('Kapasitas', $data['Kapasitas']);
        $this->db->set('kd_ruang', $data['kd_ruang']);
        // $this->db->set('JumlahKelasSerial', $data['JumlahKelasSerial']);
        // $this->db->set('JumlahPraKRS', $data['JumlahPraKRS']);
        // $this->db->set('Tugas', $data['Tugas']);
        // $this->db->set('Presensi', $data['Presensi']);
        // $this->db->set('UTS', $data['UTS']);
        // $this->db->set('UAS', $data['UAS']);
        // $this->db->set('Final', $data['Final']);
        // $this->db->set('Responsi', $data['Responsi']);
        // $this->db->set('UTSTanggal', $data['UTSTanggal']);
        // $this->db->set('UTSJamMulai', $data['UTSJamMulai']);
        // $this->db->set('UTSJamSelesai', $data['UTSJamSelesai']);
        // $this->db->set('UTSRuangID', $data['UTSRuangID']);
        // $this->db->set('UASTanggal', $data['UASTanggal']);
        // $this->db->set('UASJamMulai', $data['UASJamMulai']);
        // $this->db->set('UASJamSelesai', $data['UASJamSelesai']);
        // $this->db->set('UASRuangID', $data['UASRuangID']);
        
        $this->db->set('CUID', $this->session->userdata('user_id'));
        $this->db->set('CDATE', date('Y-m-d H:i:s'));
        
		$this->db->set('status', 'A');

        $result = $this->db->insert('tbl_jadwal');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function UpdateOnDb($data){
		//query update data		
		$this->db->flush_cache();
		// $this->db->set('id_jadwal', $data['id_jadwal']);
        // $this->db->set('JadwalPar', $data['JadwalPar']);
        // $this->db->set('JadwalSer', $data['JadwalSer']);
        $this->db->set('id_tahunajaran', $data['id_tahunajaran']);
        $this->db->set('kd_pt', $this->mdl_prosedur->getKodePT()); //inject
        $this->db->set('kd_prodi', $data['kd_prodi']);
        $this->db->set('kd_kelas', $data['kd_kelas']);
        $this->db->set('kd_jenisjadwal', $data['kd_jenisjadwal']);
        //$this->db->set('AdaResponsi', $data['AdaResponsi']);
        $this->db->set('kd_matakuliah', $data['kd_matakuliah']);
        $this->db->set('Nama', $data['Nama']);
        $this->db->set('id_hari', $data['id_hari']);
        $this->db->set('JamMulai', $data['JamMulai']);
        $this->db->set('JamSelesai', $data['JamSelesai']);
        // $this->db->set('TglMulai', $data['TglMulai']);
        // $this->db->set('TglSelesai', $data['TglSelesai']);
        $this->db->set('SKS', $data['SKS']);
        //$this->db->set('SKSHonor', $data['SKSHonor']);
        $this->db->set('kd_dosen', $data['kd_dosen']);
        $this->db->set('RencanaKehadiran', $data['RencanaKehadiran']);
        //$this->db->set('Kehadiran', $data['Kehadiran']);
        $this->db->set('KehadiranMin', $data['KehadiranMin']);
        // $this->db->set('JumlahMhsw', $data['JumlahMhsw']);
        // $this->db->set('JumlahMhswKRS', $data['JumlahMhswKRS']);
        // $this->db->set('Kapasitas', $data['Kapasitas']);
        $this->db->set('kd_ruang', $data['kd_ruang']);
        // $this->db->set('JumlahKelasSerial', $data['JumlahKelasSerial']);
        // $this->db->set('JumlahPraKRS', $data['JumlahPraKRS']);
        // $this->db->set('Tugas', $data['Tugas']);
        // $this->db->set('Presensi', $data['Presensi']);
        // $this->db->set('UTS', $data['UTS']);
        // $this->db->set('UAS', $data['UAS']);
        // $this->db->set('Final', $data['Final']);
        // $this->db->set('Responsi', $data['Responsi']);
        // $this->db->set('UTSTanggal', $data['UTSTanggal']);
        // $this->db->set('UTSJamMulai', $data['UTSJamMulai']);
        // $this->db->set('UTSJamSelesai', $data['UTSJamSelesai']);
        // $this->db->set('UTSRuangID', $data['UTSRuangID']);
        // $this->db->set('UASTanggal', $data['UASTanggal']);
        // $this->db->set('UASJamMulai', $data['UASJamMulai']);
        // $this->db->set('UASJamSelesai', $data['UASJamSelesai']);
        // $this->db->set('UASRuangID', $data['UASRuangID']);
        
        $this->db->set('MUID', $this->session->userdata('user_id'));
        $this->db->set('MDATE', date('Y-m-d H:i:s'));
		$this->db->set('status', 'A');

		$this->db->where('id_jadwal', $data['kode']);
		
        $result = $this->db->update('tbl_jadwal');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
	function DeleteOnDb($id){
		//query delete data		
		$this->db->flush_cache();
		$this->db->set('status', 'D');
		
		$this->db->set('MUID', $this->session->userdata('user_id'));
        $this->db->set('MDATE', date('Y-m-d H:i:s'));
		
		$this->db->where('id_jadwal', $id);
		
		$result = $this->db->update('tbl_jadwal');
		
		//return
		if($result) {
			return TRUE;
		}else {
			return FALSE;
		}
	}
	
}

?>